﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SqlMiM.Common.Model
{
    public class Database
    {
        public Int32 Id { get; set; }
        public String Name { get; set; }
        
        public DateTime CreatedDate { get; set; }
        
        public Byte CompatibilytLevel { get; set; }
        
        public String CollationName { get; set; }
        
        public Byte UserAccess { get; set; }
        public String UserAccessDescription { get; set; }

        public Boolean IsReadOnly { get; set; }
        public Boolean IsStandBy { get; set; }
        public Boolean IsAutoClose { get; set; }
        public Boolean IsAutoShrink { get; set; }
        public Boolean IsAutoUpdateStats { get; set; }
        public Boolean IsAutoCreateStats { get; set; }
        public Boolean IsFullTextEnabled { get; set; }

        public Byte State { get; set; }
        public String StateDescription { get; set; }
        
        public Byte SnapshotIsolationState { get; set; }
        public String SnapshotIsolationStateDescription { get; set; }
        
        public Byte RecoveryModel { get; set; }
        public String RecoveryModelDescription { get; set; }


        public static Database FillFromReader(SqlDataReader reader)
        {
            Database db = new Database();

            db.Name = reader["name"] as String;
            db.Id = (Int32)reader["database_id"];
            db.CreatedDate = reader["create_date"] != System.DBNull.Value ? (DateTime)reader["create_date"] : DateTime.Now;
            db.CompatibilytLevel = reader["compatibility_level"] != System.DBNull.Value ? (Byte)reader["compatibility_level"] : (Byte)0;
            db.CollationName = reader["collation_name"] as String;
            db.UserAccess = reader["user_access"] != System.DBNull.Value ? (Byte)reader["user_access"] : (Byte)0;
            db.UserAccessDescription = reader["user_access_desc"] as String;
            db.IsReadOnly = (Boolean)reader["is_read_only"];
            db.IsStandBy = (Boolean)reader["is_in_standby"];
            db.IsAutoClose = (Boolean)reader["is_auto_close_on"];
            db.IsAutoShrink = (Boolean)reader["is_auto_shrink_on"];
            db.State = reader["state"] != System.DBNull.Value ? (Byte)reader["state"] : (Byte)0;
            db.StateDescription = reader["state_desc"] as String;
            db.SnapshotIsolationState = reader["snapshot_isolation_state"] != System.DBNull.Value ? (Byte)reader["snapshot_isolation_state"] : (Byte)0;
            db.SnapshotIsolationStateDescription = reader["snapshot_isolation_state_desc"] as String;
            db.RecoveryModel = reader["recovery_model"] != System.DBNull.Value ? (Byte)reader["recovery_model"] : (Byte)0;
            db.RecoveryModelDescription = reader["recovery_model_desc"] as String;
            db.IsAutoCreateStats = (Boolean)reader["is_auto_create_stats_on"];
            db.IsAutoUpdateStats = (Boolean)reader["is_auto_update_stats_on"];
            db.IsFullTextEnabled = (Boolean)reader["is_fulltext_enabled"];

            return db;

        }



    }
}
